#include "../common/dataconf.h"	//compile with dataconf.cpp
#include "../common/syscomm.h"
#include "../common/generaldef.h"

#include <cstdio>
#include <cstring>

using namespace std;
using namespace syscomm;

namespace arguments{
	char datapath[PATH_LEN];
	char infiledef[NAME_LEN];
	char checker[NAME_LEN];
	char checkcomm[COMMBUF_LEN];
	char srcinput[NAME_LEN];
	char srcpath[PATH_LEN];
	bool cenamode=false;
	char srcname[NAME_LEN];
	char srctype[NAME_LEN];
	char srcbarename[NAME_LEN];
}

using namespace arguments;
using namespace fdutils;

void siginthandler(int sig){
	error("\nSIGINT caught, exiting...");
}

void check(){
	for(int i=0; i<ic; i++){

		timeval start,end;
		syscomm::clearbuf();
		sprintf(syscomm::commbuf,"./%s",srcpwd_s);


/* XXX new function has not yet been tested. */
		bool runok=true;
		gettimeofday(&start,NULL);
		int status=system(syscomm::commbuf);
		gettimeofday(&end,NULL);
		if(WIFEXITED(status)){
			if(WEXITSTATUS(status)!=0){
				printf("User program exited with a nonzero exit status.\n");
				runok=false;
			}
		}
		else if(WIFSIGNALED(status)){
			if(WTERMSIG(status)==SIGFPE) printf("User program received SIGFPE.\n");
			else if(WTERMSIG(status)==SIGINT){
				printf("User program received SIGINT.\n");
				sighandler(status);
			}
			else printf("User program received unknown signal %d\n",WTERMSIG(status));
			runok=false;
		}

		if(runok){
			printf("\033[36mTime: %.3lfs\033[0m   Checking...\n",max((end.tv_usec/1000000.0+end.tv_sec)-(start.tv_usec/1000000.0+start.tv_sec)-0.01,0.0));

			syscomm::clearbuf();
			sprintf(syscomm::commbuf,checkcomm,"std.out");
			system(syscomm::commbuf);
			if(cenamode) syscomm::showcenalog();
		}

		endscript();
	}
}
